<?php

/*
class Request, class for operating with request from client
Copyright (C) 2011  Petr Čermák

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'Controller.php';

class Request{

    const METHOD_GET = 0;
    const METHOD_POST = 1;

    private $controller = null;

    /**
     * Sets the controller associated with this request.
     * @param Controller
     */
    public function setController(Controller $controller){
        $this->controller = $controller;
    }

    public function getMethod(){
        $methodString = $_SERVER['REQUEST_METHOD'];
        if($methodString == 'POST')return self::METHOD_POST;
        return self::METHOD_GET;
    }

    public function getTime(){return $_SERVER['REQUEST_TIME'];}
    public function getQueryString(){return $_SERVER['QUERY_STRING'];}
    public function getHttpAccept(){return $_SERVER['HTTP_ACCEPT'];}
    public function getHttpAcceptCharset(){return $_SERVER['HTTP_ACCEPT_CHARSET'];}
    public function getHttpAcceptEncoding(){return $_SERVER['HTTP_ACCEPT_ENCODING'];}
    public function getHttpAcceptLanguage(){return $_SERVER['HTTP_ACCEPT_LANGUAGE'];}
    public function getHttpConnection(){return $_SERVER['HTTP_CONNECTION'];}
    public function getHttpHost(){return $_SERVER['HTTP_HOST'];}
    public function getHttpReferer(){return $_SERVER['HTTP_REFERER'];}
    public function getHttpUserAgent(){return $_SERVER['HTTP_USER_AGENT'];}
    public function isHttps(){return isset($_SERVER['HTTPS']);}
    public function getRemoteAddress(){return $_SERVER['REMOTE_ADDR'];}
    public function getRemoteHost(){return $_SERVER['REMOTE_HOST'];}
    public function getRemotePort(){return $_SERVER['REMOTE_PORT'];}
    public function getURI(){return $_SERVER['REQUEST_URI'];}

    public function hasCookie($name){return isset($_COOKIE[$name]);}

    public function getCookie($name){
        if(isset($_COOKIE[$name]))
            return $_COOKIE[$name];
        return null;
    }

    /**
     * Checks if this request contains parameter of provided name.
     * @param string  name of the parameter
     * @param integer  request method, can be Request::GET, Request::POST
     * or null, in which case both method arrays are searched, default null.
     * @return bool  if the parameter was found
     */
    public function hasParameter($name, $method = null){
        Checker::checkString($name);
        if($method == null){
            if(isset($_GET[$name]))return true;
            if(isset($_POST[$name]))return true;
            return false;
        }
        if($method == self::METHOD_POST && isset($_POST[$name]))
            return true;
        else if($method == self::METHOD_GET && isset($_GET[$name]))return true;
        return false;
    }

    /**
     * Returns value of request parameter of provided name.
     * @param string  name of the parameter
     * @param integer  request method, can be Request::GET, Request::POST
     * or null, in which case both method arrays are searched, default null.
     * @return string|null value of the parameter, null if not found.
     */
    public function getParameter($name, $method = null){
        Checker::checkString($name);
        $ret = null;
        if($method == null){
            if(isset($_GET[$name]))$ret = $_GET[$name];
            else if(isset($_POST[$name]))return $_POST[$name];
        }
        else{
            if($method == self::METHOD_POST && isset($_POST[$name]))
                return $_POST[$name];
            else if($method == self::METHOD_GET && isset($_GET[$name]))
                $ret = $_GET[$name];
        }
        if($ret !== null && $this->controller !== null)
            $this->controller->reserveParameter($name);
        return $ret;
    }

}
?>
